package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.ChangePlaceTypeEnum;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.SpChangePlaceCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpChangePlaceResult;
import com.engine4cloud.tdf.supervision.dto.update.SpChangePlaceUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpChangePlaceEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpChangePlaceConvert {
    SpChangePlaceConvert INSTANCE = Mappers.getMapper(SpChangePlaceConvert.class);

    SpChangePlaceEntity createTo(SpChangePlaceCreateDTO s);

    SpChangePlaceEntity updateTo(SpChangePlaceUpdateDTO s);

    SpChangePlaceResult convertTo(SpChangePlaceEntity s);

    default SpChangePlaceEntity create(SpChangePlaceCreateDTO s, Long tenantId, NormalAuditActionStatusEnum action) {
        SpChangePlaceEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setPreAction(action);
        additionalInformation(entity, s.getDestinationList());
        return entity;
    }

    default void additionalInformation(SpChangePlaceEntity t, List<String> destinationList) {
        if (Objects.isNull(t) || CollectionUtils.isEmpty(destinationList)) {
            return;
        }

        t.setQrdszs(destinationList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        t.setQrdszd(destinationList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (destinationList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            t.setQrdszx(destinationList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            t.setQrdszx(null);
        }

        if (destinationList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            t.setQrdxz(destinationList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            t.setQrdxz(null);
        }
    }

    default SpChangePlaceEntity update(SpChangePlaceUpdateDTO s) {
        SpChangePlaceEntity entity = updateTo(s);
        additionalInformation(entity, s.getDestinationList());
        return entity;
    }

    default SpChangePlaceResult convert(SpChangePlaceEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }

        SpChangePlaceResult result = convertTo(s);
        List<String> destinationList = new ArrayList<>();
        destinationList.add(s.getQrdszs());
        destinationList.add(s.getQrdszd());
        Actions.accept(s.getQrdszx(), v -> destinationList.add(s.getQrdszx()));
        Actions.accept(s.getQrdxz(), v -> destinationList.add(s.getQrdxz()));
        result.setDestinationList(destinationList);
        return result;
    }

    default SpChangePlaceResult convertCategory(SpChangePlaceEntity s) {
        if (Objects.isNull(s)) {
            return null;
        }

        SpChangePlaceResult result = convertTo(s);
        result.setBglx(ChangePlaceTypeEnum.valueOf(result.getBglx()).getDesc());
        return result;
    }
}
